package co.edu.unal.geditor.view.tools;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.SwingUtilities;

import co.edu.unal.geditor.controller.App;
import co.edu.unal.geditor.controller.action.tool.BaseToolAction;
import co.edu.unal.geditor.model.GraphElement;
import co.edu.unal.geditor.view.Canvas;

public abstract class Tool
{
	public abstract void processMouseReleased( MouseEvent me );
	
	protected abstract void initAction();

	public Tool()
	{
		initAction();

		float[] dashPattern = {
				4.0f, 4.0f
		};

		m_dashStroke = new BasicStroke( 1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dashPattern, 0.0f );
	}

	public void setAction( BaseToolAction action )
	{
		assert action != null;

		m_action = action;
	}

	public BaseToolAction getAction()
	{
		return m_action;
	}

	public void processMousePressed( Point ptPressed )
	{
		m_ptPressed = ptPressed;
		m_ptCurrent = ptPressed;
	}
	
	public void processMouseMoved (Point ptActualPos){
		
	}

	protected void drawFeedback( Graphics2D g )
	{
		
		int x1,y1,x2,y2;
		if (m_ptPressed.x < m_ptCurrent.x )
			{
			 x1 = m_ptPressed.x;
			 x2 = m_ptCurrent.x;
			}
		 else
		 {
			 x1=m_ptCurrent.x;
			 x2 = m_ptPressed.x;
		 }
		if (m_ptPressed.y < m_ptCurrent.y )
		{
			 y1 = m_ptPressed.y; 
			 y2 = m_ptCurrent.y;
		}
		else
		{
			y1 = m_ptCurrent.y;
			y2 = m_ptPressed.y; 

		}
		
		/*int w = Math.abs( m_ptCurrent.x - m_ptPressed.x );
		int h = Math.abs( m_ptCurrent.y - m_ptPressed.y );

		g.drawRect( x, y, w, h );*/
		App.getInstance().getCanvas().getActiveFeedbackGraphElement().getBoundBox().setLeftUp(new Point(x1,y1));
		App.getInstance().getCanvas().getActiveFeedbackGraphElement().getBoundBox().setRightDown(new Point(x2,y2));
		App.getInstance().getCanvas().getActiveFeedbackGraphElement().setLinesColor(Color.LIGHT_GRAY);
		App.getInstance().getCanvas().getActiveFeedbackGraphElement().paint(g);
	}

	// template method design pattern
	public void processMouseDragged( MouseEvent me)
	{
		if (SwingUtilities.isLeftMouseButton(me)){
			Point pt=me.getPoint();
			Canvas canvas = App.getInstance().getCanvas();
		
			Graphics2D g = (Graphics2D)canvas.getGraphics();
			g.setColor( Color.LIGHT_GRAY );
			g.setXORMode( Color.WHITE );
			g.setStroke( m_dashStroke );

			drawFeedback( g );
			m_ptCurrent = pt;
			drawFeedback( g );
		}
	}
	
	public void setActiveAction(int Action){
		ActiveAction=Action;
	}
	
	public void setActiveFeedbackGraphElement(){
		App.getInstance().getCanvas().setActiveFeedbackGraphElement(feedbackGraphElemet);
	}
	protected GraphElement feedbackGraphElemet;
	private BaseToolAction m_action;
	protected int ActiveAction=0;
	protected Point m_ptPressed;
	protected Point m_ptCurrent;
	protected BasicStroke m_dashStroke;
	public static final int SELECT=0;
	public static final int MOVE=1;
	public static final int RESIZE=2;
}
